package com.aisino.utils;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;

import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;

/**
 * AES加密解密的工具类
 */
@Component
public class VueJavaCryptoUtil {
    private static final Logger log = LoggerFactory.getLogger(VueJavaCryptoUtil.class);

    public static String aesKeySource;

//    @Value("${aes.keySource}")
//    private String aesKey;
//
//    @PostConstruct
//    public void init() {
//        VueJavaCryptoUtil.aesKeySource = aesKey;
//    }


    @Value("${aes.keySource}")
    public void setAesKeySource(String AesKeySource) {
        aesKeySource = AesKeySource;
    }

    public static void main(String[] args) {
        String content = "test中文";
        String keySource = "xiaoguozhenshuai";
        String encrypt = aisinoEncrypt(content, keySource);
        System.out.println("encrypt = " + encrypt);
        String decrypt = aisinoDecrypt(encrypt, keySource);
        System.out.println("decrypt = " + decrypt);
        String decryptBak = aisinoDecrypt("Dkxa7mE0+AE1Hy+8vKrtoA==", keySource);
        System.out.println("decryptBak = " + decryptBak);
    }

    /**
     * 自定义加密
     * 首先通过aes算法加密
     * 然后通过base64算法加密
     *
     * @param content 明文
     * @param AesKey  密钥 长度只能是 16对应AES-128   24对应AES-192   32对应AES-256
     * @return
     */
    public static String aisinoEncrypt(String content, String AesKey) {
        if (AesKey == null || !(AesKey.length() == 16 || AesKey.length() == 24 || AesKey.length() == 32)) {
            throw new RuntimeException("AESKey长度不正确");
        }
        byte[] keySource = AesKey.getBytes(StandardCharsets.UTF_8);
        // 构建aes
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, keySource);
        // aes加密
        byte[] encrypt = aes.encrypt(content);
        // base64加密
        String encodeBase64 = Base64Utils.encodeToString(encrypt);
        log.info("加密后：{}", encodeBase64);
        return encodeBase64;
    }

    /**
     * 自定义解密
     * 首先通过base64算法解密
     * 然后通过aes算法解密
     *
     * @param base64Str base64密文
     * @param AesKey    密钥 长度只能是 16对应AES-128   24对应AES-192   32对应AES-256
     * @return
     */
    public static String aisinoDecrypt(String base64Str, String AesKey) {
        if (AesKey == null || !(AesKey.length() == 16 || AesKey.length() == 24 || AesKey.length() == 32)) {
            throw new RuntimeException("AESKey长度不正确");
        }
        byte[] keySource = AesKey.getBytes(StandardCharsets.UTF_8);
        // base64解密
        byte[] decodeBase64 = Base64Utils.decodeFromString(base64Str);
        // 构建aes
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, keySource);
        // aes解密
        byte[] decrypt = aes.decrypt(decodeBase64);
        String decryptstr = StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8);
        log.info("解密后的字符串:{}", decryptstr);
        // 返回解密后的字符串
        return decryptstr;
    }

    /**
     * 传入base64密文，返回加密后的字符串
     * @param base64Str 密文
     * @return 加密后结果
     */
    public static String getEncryptResult(String base64Str){
        return aisinoEncrypt(base64Str,aesKeySource);
    }

    /**
     * 传入base64密文，返回解密后的字符串
     * @param base64Str 密文
     * @return 解密后结果
     */
    public static String getDecryptResult(String base64Str){
        return aisinoDecrypt(base64Str,aesKeySource);
    }
}
